package com.example.lin.week2project.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.lin.week2project.R;
import com.example.lin.week2project.pojo.QingChiFan;
import com.example.lin.week2project.utils.DateUtils;

import java.util.List;

public class WeChatAdpater extends BaseAdapter {

    private Context mContext;

    private QingChiFan qingChiFan;

    public WeChatAdpater(Context mContext, QingChiFan qingChiFan) {
        this.mContext = mContext;
        this.qingChiFan = qingChiFan;
    }

    @Override
    public int getCount() {
        List<QingChiFan.DataBean.ResultsBean> results = qingChiFan.getData().getResults();
        return results == null ? 0 : results.size();
    }

    @Override
    public QingChiFan.DataBean.ResultsBean getItem(int position) {
        return qingChiFan.getData().getResults().get(position);
    }

    private static class ViewHolder {
        ImageView iv_icon;
        TextView tv_time;
        TextView tv_title;
        TextView tv_lastMsg;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.view_wechat_list_item1, parent, false);
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.iv_icon = convertView.findViewById(R.id.iv_icon);
            viewHolder.tv_time = convertView.findViewById(R.id.tv_time);
            viewHolder.tv_title = convertView.findViewById(R.id.tv_title);
            viewHolder.tv_lastMsg = convertView.findViewById(R.id.tv_lastMsg);
            convertView.setTag(viewHolder);
        }
        ViewHolder vh = (ViewHolder) convertView.getTag();
        QingChiFan.DataBean.ResultsBean item = getItem(position);
        vh.tv_title.setText(item.getEventLocation());
        vh.tv_lastMsg.setText(item.getEventDescription());
        vh.tv_time.setText(DateUtils.getWeChatTime(item.getCreateTime()));

        //TODO 头像显示
        String userImageUrl = item.getUser().getUserImageUrl();
        Glide.with(mContext).load(userImageUrl).into(vh.iv_icon);
        return convertView;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

}
